﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Collections;
namespace TS_XY_Train_Repaire.Locomotive.MainBase
{
    public partial class Locomotive_MainBase_TrainDepSys_ManaForm : DevExpress.XtraEditors.XtraForm
    {
        public Locomotive_MainBase_TrainDepSys_ManaForm()
        {
            InitializeComponent();
        }

        private void Locomotive_MainBase_TrainDepSys_ManaForm_Load(object sender, EventArgs e)
        {
            _LoadTreeList();
        }

        private void Locomotive_MainBase_TrainDepSys_ManaForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            TS_Win_Locomotive_MainForm.htActionItem.Remove(this.Name);
        }
        #region//--机车零部件分类事件--//
        private void _LoadTreeList()
        {
            LeverString_TrainDepSysList _tl = new LeverString_TrainDepSysList();
            tl_Main.Nodes.Clear();
            ArrayList pList = new ArrayList();
            string strSQL = "select * from TS_S_T_P_TrainClass where f_del=0";
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand cmd = new SqlCommand(strSQL, conn);
            SqlDataReader read = cmd.ExecuteReader();
            while (read.Read())
            {
                _tl = new LeverString_TrainDepSysList();
                _tl.f_id = "M" + read["f_id"].ToString();
                _tl.f_tcid = read["f_id"].ToString();
                _tl.f_name = read["f_name"].ToString();
                _tl.f_deep = "-1";
                _tl.f_level = ",";
                pList.Add(_tl);
            }
            read.Close();
            strSQL = "select f_name,f_id,f_tcid from TS_S_T_P_TrainType where f_del=0";
            cmd = new SqlCommand(strSQL, conn);
            read = cmd.ExecuteReader();
            while (read.Read())
            {
                _tl = new LeverString_TrainDepSysList();
                _tl.f_id = "T" + read["f_id"].ToString();
                _tl.f_tcid = read["f_tcid"].ToString();
                _tl.f_ttid = read["f_id"].ToString();
                _tl.f_name = read["f_name"].ToString();
                _tl.f_parent = "M" + read["f_tcid"].ToString();
                _tl.f_deep = "-1";
                _tl.f_level = ",";
                pList.Add(_tl);
            }
            read.Close();
            strSQL = "select *,(case f_type when 0 then '总成' when 1 then '转向架' when 2 then '电器' when 9 then '通用' else '未知' end ) as f_typename from TS_S_T_P_TrainDepSys where f_deep=0 and f_del=0";
            cmd = new SqlCommand(strSQL, conn);
            read = cmd.ExecuteReader();
            string _strUPID = "and (";
            int i = 0;
            while (read.Read())
            {
                _tl = new LeverString_TrainDepSysList();
                _tl.f_id = "D" + read["f_id"].ToString();
                _tl.f_tcid = read["f_tcid"].ToString();
                _tl.f_ttid = read["f_ttid"].ToString();
                _tl.f_code = read["f_code"].ToString();
                _tl.f_name = read["f_name"].ToString();
                _tl.f_name1 = read["f_name1"].ToString();
                _tl.f_parent = "T"+ read["f_ttid"].ToString();
                _tl.f_deep = read["f_deep"].ToString();
                _tl.f_level = read["f_level"].ToString();
                _tl.f_num = read["f_num"].ToString();
                _tl.f_type= read["f_type"].ToString();
                _tl.f_typename = read["f_typename"].ToString();
                _tl.f_indate= read["f_indate"].ToString();
                if (i == 0)
                {
                    _strUPID += "(f_level like '%," + read["f_id"].ToString() + ",%')";
                }
                else
                {
                    _strUPID += "or (f_level like '%," + read["f_id"].ToString() + ",%')";
                }
                pList.Add(_tl);
                i++;
            }
            _strUPID += ")";
            if (_strUPID.Length > 7)
            {
                read.Close();
                strSQL = "select *,(case f_type when 0 then '总成' when 1 then '转向架' when 2 then '电器' when 9 then '通用' else '未知' end ) as f_typename from TS_S_T_P_TrainDepSys where f_deep>0 and f_del=0 " + _strUPID + " order by f_deep,f_level,f_no asc";
                cmd = new SqlCommand(strSQL, conn);
                read = cmd.ExecuteReader();
                string _UPID = "";
                while (read.Read())
                {
                    _tl = new LeverString_TrainDepSysList();
                    _tl.f_id = "D" + read["f_id"].ToString();
                    _tl.f_tcid = read["f_tcid"].ToString();
                    _tl.f_ttid = read["f_ttid"].ToString();
                    _tl.f_code = read["f_code"].ToString();
                    _tl.f_name = read["f_name"].ToString();
                    _tl.f_name1 = read["f_name1"].ToString();
                    _tl.f_deep = read["f_deep"].ToString();
                    _tl.f_level = read["f_level"].ToString();
                    _tl.f_num = read["f_num"].ToString();
                    _tl.f_indate = read["f_indate"].ToString();
                    _tl.f_type = read["f_type"].ToString();
                    _tl.f_typename = read["f_typename"].ToString();
                    _UPID = read["f_level"].ToString();
                    _UPID = _UPID.Substring(0, _UPID.Length - 1);
                    _UPID = _UPID.Substring(_UPID.LastIndexOf(',') + 1);
                    _tl.f_parent = "D" + _UPID;
                    pList.Add(_tl);
                }
            }
            read.Close();
            conn.Close();
            conn.Dispose();
            tl_Main.DataSource = pList;
        }
        private void tl_Main_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            bbi_DepSys_Edit.Enabled = false;
            bbi_DepSys_Del.Enabled = false;
            bbi_WorkPlace_Add.Enabled = false;
            bbi_WorkPlace_Add.Tag = "";
            bbi_DepSys_Type.Enabled = false;
            try
            {
                if (tl_Main.FocusedNode["f_id"].ToString().Substring(0,1)=="D")
                {
                    bbi_DepSys_Edit.Enabled = true;
                    bbi_DepSys_Del.Enabled = true;
                    //if (tl_Main.FocusedNode["f_id"].ToString() == "D0"|| tl_Main.FocusedNode["f_id"].ToString() == "D1" || tl_Main.FocusedNode["f_id"].ToString() == "D2")
                    //{
                    //    bbi_DepSys_Del.Enabled = false;
                    //}
                    //else
                    //{
                    //    bbi_DepSys_Del.Enabled = true;
                    //}
                    if (tl_Main.FocusedNode["f_id"].ToString().Substring(0, 1) == "D")
                    //if (tl_Main.FocusedNode["f_id"].ToString().Substring(0, 1) == "D" && (tl_Main.FocusedNode["f_level"].ToString().IndexOf(",2,") >= 0 || tl_Main.FocusedNode["f_level"].ToString().IndexOf(",1,") >= 0))
                    {
                        bbi_DepSys_Type.Enabled = true;
                    }
                    else
                    {
                        bbi_DepSys_Type.Enabled = false;
                    }
                    bbi_WorkPlace_Add.Enabled = true;
                    bbi_WorkPlace_Add.Tag = "D";
                }
                if (tl_Main.FocusedNode["f_id"].ToString().Substring(0, 1) == "T")
                {
                    bbi_WorkPlace_Add.Enabled = true;
                    bbi_WorkPlace_Add.Tag = "T";
                }
            }
            catch
            {
                bbi_DepSys_Edit.Enabled = false;
                bbi_DepSys_Del.Enabled = false;
                bbi_WorkPlace_Add.Enabled = false;
            }
        }

        private void tl_Main_DoubleClick(object sender, EventArgs e)
        {
            if (tl_Main.FocusedNode["f_id"].ToString().Substring(0, 1) == "D")
            {
                _LoadGrid();
            }
            else
            {
                gc_Main.DataSource = null;
                gc_Main.Refresh();
            }
        }
        #endregion
        #region//--机车项点事件--//
        private void _LoadGrid()
        {
            string strSQL = "select *,(select f_name from TS_S_T_P_TrainClass where f_id=TS_S_T_P_Train_WorkPlace.f_tcid) as f_tcname,(select f_name from TS_S_T_P_TrainPerf where f_id=TS_S_T_P_Train_WorkPlace.f_perfid) as f_perfname,(select f_name from TS_S_T_B_TrainMust where f_id=TS_S_T_P_Train_WorkPlace.f_must) as f_mustname,(select f_name from TS_S_T_B_TrainRank where f_id=TS_S_T_P_Train_WorkPlace.f_rank) as f_rankname,(select f_name from TS_S_T_B_TrainDataType where f_id=TS_S_T_P_Train_WorkPlace.f_dtid) as f_dtname,(select f_name from TS_S_T_B_TrainRank where f_id=TS_S_T_P_Train_WorkPlace.f_rank) as f_rankname,(select f_name from TS_S_T_P_WorkPlaceType where f_id=TS_S_T_P_Train_WorkPlace.f_typeid) as f_typename,(select f_name from TS_S_T_P_TrainDepSys where f_id=TS_S_T_P_Train_WorkPlace.f_tdid) as f_tdname from TS_S_T_P_Train_WorkPlace where f_del=0 and f_tdid in (select f_id from TS_S_T_P_TrainDepSys where f_del=0 and (f_id=" + tl_Main.FocusedNode["f_id"].ToString().Substring(1) + " or f_level like '%," + tl_Main.FocusedNode["f_id"].ToString().Substring(1) + ",%'))";
            DataTable dt = new DataTable();
            TS_Win_Class.FilledDataTable(strSQL, dt);
            gc_Main.DataSource = dt;
        }
        private void gc_Main_Click(object sender, EventArgs e)
        {
            bbi_WorkPlace_Edit.Enabled = false;
            bbi_WorkPlace_Del.Enabled = false;
            if (gv_Main.SelectedRowsCount>0)
            {
                bbi_WorkPlace_Add.Tag = "W";
                bbi_WorkPlace_Edit.Enabled = true;
                bbi_WorkPlace_Del.Enabled = true;
            }
        }
        #endregion
        private void bbi_Reload_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                string _NowID = tl_Main.FocusedNode["f_id"].ToString();
                _LoadTreeList();
                tl_Main.SetFocusedNode(tl_Main.FindNodeByFieldValue("f_id", _NowID));
                _LoadGrid();
            }
            catch
            {
            }
        }
        #region//--零部件分类管理--//
        private void bbi_DepSys_Add_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string _NowID = tl_Main.FocusedNode["f_id"].ToString();
            UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_TrainDepSys_ManaForm _uc = new UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_TrainDepSys_ManaForm();
            _uc._LoadType = "Add";
            _uc._LoadID = tl_Main.FocusedNode["f_id"].ToString().Substring(1);
            _uc._LoadLevel = tl_Main.FocusedNode["f_level"].ToString();
            _uc._LoadDeep= tl_Main.FocusedNode["f_deep"].ToString();
            _uc._LoadTCID = tl_Main.FocusedNode["f_tcid"].ToString();
            _uc._LoadTTID = tl_Main.FocusedNode["f_ttid"].ToString();
            if (_uc.ShowDialog() == DialogResult.OK)
            {
                _LoadTreeList();
                tl_Main.SetFocusedNode(tl_Main.FindNodeByFieldValue("f_id", _NowID));
            }
        }

        private void bbi_DepSys_Edit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string _NowID = tl_Main.FocusedNode["f_id"].ToString();
            UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_TrainDepSys_ManaForm _uc = new UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_TrainDepSys_ManaForm();
            _uc._LoadType = "Edit";
            _uc._LoadID = tl_Main.FocusedNode["f_id"].ToString().Substring(1);
            _uc._LoadLevel = tl_Main.FocusedNode["f_level"].ToString();
            _uc._LoadDeep = tl_Main.FocusedNode["f_deep"].ToString();
            _uc._LoadTCID = tl_Main.FocusedNode["f_tcid"].ToString();
            if (_uc.ShowDialog() == DialogResult.OK)
            {
                _LoadTreeList();
                tl_Main.SetFocusedNode(tl_Main.FindNodeByFieldValue("f_id", _NowID));
            }
        }
        private void bbi_DepSys_Del_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string _NowID = tl_Main.FocusedNode.ParentNode["f_id"].ToString();
            UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_TrainDepSys_ManaForm _uc = new UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_TrainDepSys_ManaForm();
            _uc._LoadType = "Del";
            _uc._LoadID = tl_Main.FocusedNode["f_id"].ToString().Substring(1);
            _uc._LoadLevel = tl_Main.FocusedNode["f_level"].ToString();
            _uc._LoadDeep = tl_Main.FocusedNode["f_deep"].ToString();
            _uc._LoadTCID = tl_Main.FocusedNode["f_tcid"].ToString();
            if (_uc.ShowDialog() == DialogResult.OK)
            {
                _LoadTreeList();
                tl_Main.SetFocusedNode(tl_Main.FindNodeByFieldValue("f_id", _NowID));
            }
        }
        private void bbi_DepSys_Type_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            TS_XY_Train_Repaire.UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_TrainDepSys_Type _uc = new UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_TrainDepSys_Type();
            _uc._LoadID = tl_Main.FocusedNode["f_id"].ToString().Substring(1);
            _uc.ShowDialog();
        }

        private void bbi_DepSys_State_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            TS_XY_Train_Repaire.UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_TrainDepSys_State _uc = new UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_TrainDepSys_State();
            _uc.ShowDialog();
        }

        private void bbi_DepSys_Source_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            TS_XY_Train_Repaire.UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_TrainDepSys_Source _uc = new UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_TrainDepSys_Source();
            _uc.ShowDialog();
        }
        #endregion
        #region//--作业项点管理--//
        private void bbi_WorkPlace_Add_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_Train_WorkPlace_ManaForm _uc = new UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_Train_WorkPlace_ManaForm();
            if (bbi_WorkPlace_Add.Tag.ToString() == "D")
            {
                _uc._LoadType = "Add";
                _uc._LoadID = tl_Main.FocusedNode["f_id"].ToString();
                _uc._LoadTCID = tl_Main.FocusedNode["f_tcid"].ToString();
                _uc._LoadTDID = tl_Main.FocusedNode["f_id"].ToString().Substring(1);
            }
            if (bbi_WorkPlace_Add.Tag.ToString() == "W")
            {
                _uc._LoadType = "Add";
                _uc._LoadID = gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_id").ToString();
                _uc._LoadTCID = gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_tcid").ToString();
                _uc._LoadTDID = gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_tdid").ToString();
            }
            _uc.ShowDialog();
            _LoadGrid();
        }

        private void bbi_WorkPlace_Edit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_Train_WorkPlace_ManaForm _uc = new UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_Train_WorkPlace_ManaForm();
            _uc._LoadType = "Edit";
            _uc._LoadID = gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_id").ToString();
            _uc._LoadTCID = gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_tcid").ToString();
            _uc._LoadTDID = gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_tdid").ToString();
            _uc.ShowDialog();
            _LoadGrid();
        }

        private void bbi_WorkPlace_Del_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_Train_WorkPlace_ManaForm _uc = new UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_Train_WorkPlace_ManaForm();
            _uc._LoadType = "Del";
            _uc._LoadID = gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_id").ToString();
            _uc._LoadTCID = gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_tcid").ToString();
            _uc._LoadTDID = gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_tdid").ToString();
            _uc.ShowDialog();
            _LoadGrid();
        }
        #endregion
        #region//--机车部位管理--//
        private void bbi_TrainPart_Mana_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_TrainPart _uc = new UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_TrainPart();
            _uc.ShowDialog();
        }
        #endregion
        #region//--机车专业管理--//
        private void bbi_TrainPerf_Mana_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_Trainperf _uc = new UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_Trainperf();
            _uc.ShowDialog();
        }
        #endregion
        #region//--作业项点基础字典--//
        private void bbi_WorkPlaceType_Mana_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            TS_XY_Train_Repaire.UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_TrainDepSys_WorkPlaceType _uc = new UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_TrainDepSys_WorkPlaceType();
            _uc.ShowDialog();
        }

        private void bbi_TrainRank_Mana_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            TS_XY_Train_Repaire.UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_TrainDepSys_TrainRank _uc = new UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_TrainDepSys_TrainRank();
            _uc.ShowDialog();
        }

        private void bbi_TrainMust_Mana_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            TS_XY_Train_Repaire.UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_TrainDepSys_TrainMust _uc = new UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_TrainDepSys_TrainMust();
            _uc.ShowDialog();
        }

        private void bbi_TrainDataResult_Mana_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            TS_XY_Train_Repaire.UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_TrainDepSys_TrainDataResult _uc = new UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_TrainDepSys_TrainDataResult();
            _uc.ShowDialog();
        }

        private void bbi_TrainDataType_Mana_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            TS_XY_Train_Repaire.UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_TrainDepSys_TrainDataType _uc = new UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_TrainDepSys_TrainDataType();
            _uc.ShowDialog();
        }

        private void bbi_TrainDataUnit_Mana_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            TS_XY_Train_Repaire.UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_TrainDepSys_TrainDataUnit _uc = new UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_TrainDepSys_TrainDataUnit();
            _uc.ShowDialog();
        }
        #endregion
        #region//--机车修程管理--//
        private void bbi_TrainRepair_Mana_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_TrainRepair _uc = new UserCon.Locomotive.MainBase.TrainDepSys.uc_Locomotive_MainBase_TrainRepair();
            _uc.ShowDialog();
        }
        #endregion
    }
}